package com.test.study;

import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.ListPosition;
import redis.clients.jedis.Tuple;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Jedisdemo {
    Jedis jedis = null;

    @Before
    public void before() {
        jedis = new Jedis("49.233.198.42", 6381);
        jedis.auth("!QAZ2wsx3edc");
    }

    @Test
    public void testGenerl() {
        Set<String> keys = jedis.keys("*");
      //  keys.forEach(System.out::println);
        System.out.println(jedis.ttl("username"));
        Long username = jedis.expire("username", 50);
       System.out.println(username);
        Long username1 = jedis.persist("username");
        System.out.println(username1);

    }

    @Test
    public void testString() {
        String set = jedis.set("username", "huyong");
        System.out.println(set);
        String username = jedis.get("username");
        System.out.println(username);
        String mset = jedis.mset("password", "!QAZ2wsx", "age", "26", "address", "xian");
        System.out.println(mset);
        System.out.println(jedis.mget("password", "age", "address"));
        Long append = jedis.append("username", " love weijiaojiao");
        System.out.println(append);
        username = jedis.get("username");
        System.out.println(username);
        Long setrange = jedis.setrange("username", 6, " love ");
        System.out.println(setrange);
        username = jedis.get("username");
        System.out.println(username);
        System.out.println(jedis.getrange("password", 4, -1));

    }

    @Test
    public void testList() {
        Long lpush = jedis.lpush("names", "huyong", "weijiaojiao", "laojia");
        System.out.println(lpush);
        List<String> names = jedis.lrange("names", 0, -1);
        System.out.println(names);
        String lset = jedis.lset("names", 0, "whx");
        System.out.println(lset);
    }

    @Test
    public void testList2() {
        List<String> names = jedis.lrange("names", 0, -1);
        System.out.println(names);
        String lset = jedis.lset("names", 0, "whx");
        System.out.println(lset);
        System.out.println(jedis.lindex("names", 3));
        Long lrem = jedis.lrem("names", 3, "laojia");
        System.out.println(lrem);
        Long linsert = jedis.linsert("names", ListPosition.BEFORE, "laojia", "jiehun");
        System.out.println(linsert);
        System.out.println(jedis.lrange("names", 0, -1));
    }

    @Test
    public void testSet() {
        Long sadd = jedis.sadd("language1", "java", "C++", "shell");
        System.out.println(sadd);
        System.out.println(jedis.smembers("language1"));
        System.out.println(jedis.srem("language1","C++"));
        System.out.println(jedis.smembers("language1"));
        jedis.sadd("e1","1","2","3","4","5","6","7");
        jedis.sadd("e2","4","5","6","7","8","9","10");
        System.out.println(jedis.sdiff("e1","e2"));
        System.out.println(jedis.sinter("e1","e2"));
        System.out.println(jedis.sunion("e1","e2"));
    }

    @Test
    public void testZset() {
        jedis.zadd("zset",1,"one1");
        Map<String,Double> map=new HashMap<>();
        map.put("张三",60.0);
        map.put("李四",77.0);
        map.put("王五",98.0);
        map.put("赵六",33.0);
        jedis.zadd("score",map);


        System.out.println(jedis.zrange("zset",0,-1));
        Set<String> score = jedis.zrangeByScore("score", 60, 90);
        System.out.println(score);
        Set<Tuple> score1 = jedis.zrangeWithScores("score", 0, -1);
        for (Tuple tuple:score1){
            System.out.println(tuple.getElement()+"=="+ tuple.getScore());
        }

        System.out.println(jedis.zrank("score","张三"));
        System.out.println(jedis.zscore("score","张三"));
        jedis.zrem("score","赵六");
        System.out.println(jedis.zrange("zset",0,-1));

    }

    @Test
    public void testHash() {
        Long hset = jedis.hset("person", "username", "huyong");
        System.out.println(hset);
        System.out.println(jedis.hget("person","username"));
        Map<String, String> map=new HashMap<>();
        map.put("usernmae","huyong");
        map.put("address","西安");
        map.put("sex","爷们");
        String user = jedis.hmset("user", map);
        System.out.println(user);
        System.out.println(jedis.hmget("user","usernmae","sex"));
        System.out.println(jedis.hgetAll("user"));
        System.out.println(jedis.hkeys("user"));
        System.out.println(jedis.hvals("user"));
        System.out.println(jedis.hdel("user","sex"));

    }

}
